package uk.ac.aber.paws.client.core;

/**
 * @author Rob Johnson, Aberystwyth University
 * @email rob.johnson@aber.ac.uk
 * @date 8th August 2011
 * 
 * Initiator.java
 * 
 * Top level class for running the various sub applications contained within this JAR. These options include:
 * 
 * a) PAWSClient - the main PAWS Client application that runs within the wrapper
 * b) PenDriveChecker - this is run by the o/s at logout time to check the existence of orphaned pen drive
 * c) IdleLogger - run at login time for each user (via registry) that reports idle times to the PAWS Client service
 * d) LogMeOut - run at login time for each user. When a user is inactive for set period, the application automatically logs out the user
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import uk.ac.aber.paws.client.constants.Constants;
import uk.ac.aber.paws.client.idlelogger.IdleLogger;
import uk.ac.aber.paws.client.logmeout.LogMeOut;
import uk.ac.aber.paws.client.pendrivechecker.PenDriveChecker;

public class Initiator {

	public static Logger LOG_FILE;
	private static String key = null;
	
	public static void main(String args[]) {
		
		if (args.length > 0) {
			
			// extract the type of app to be run:
			key = args[0];
		
			// Strip out the 1st element of the array - not needed  
			String[] newArgs = new String[args.length-1];
			for (int i=1;i<args.length;i++) {
				newArgs[i-1] = args[i];
			}
		
			// run the pen drive checker program
			if (key.equals("pendrivechecker")) {
				PenDriveChecker.main(newArgs);
			}
			
			// run the idle logger program
			else if (key.equals("idlelogger")) {
				IdleLogger.main(newArgs);
			}
			
			// run the logmeout auto logout program
			else if (key.equals("logmeout")) {
				LogMeOut.main(newArgs);
			}
			
			else if (key.equals("stoppaws")) {
				PAWSClientStopper.main(newArgs);
			}
			
			// runs the PAWS Client wrapper service
			else if (key.equals("pawsclient")) {
				
				/*if (newArgs.length > 1) {
					try {
						startLogFile(newArgs[1]);
					}
					catch (IndexOutOfBoundsException ex) {
						System.out.println("Incorrectly specified parameters");
						System.exit(1);
					}
				}*/
				
				PAWSClient.main(newArgs);
			}
		}
	}
	
	/**
	 * This is called buy the YAJSW wrapper when the service is undergoing termination. Significant
	 * closing down procedures for the application should be initiated here. This is an alternative to
	 * shutdown hooks that do not otherwise work when this app is called within the wrapper.
	 * @param code This is the shutdown code supplied by the windows operating system. 1 = manual service 
	 * restart/shutdown, 4 = operating system restart/shutdown
	 */
	public static void handleWrapperShutdown(String code) {
		
		PAWSClient.stop(code); 
		
		Initiator.LOG_FILE.log(Level.ALL,"into Initiator");
		
		if (LOG_FILE != null) {
			
			Initiator.LOG_FILE.log(Level.ALL,"service shutdown code="+code);
		}
	}
	
	/**
	 * Initiates the log file based off the required level
	 * @param logLevel The log level required: LEVEL0 = very limited logging (should be default) to
	 * LEVEL4 = log everything.
	 */
	public static void startLogFile(String logLevel, String os) {
		
		Level logging = Level.OFF;
		
		if (logLevel != null) {
			
			if (logLevel.equals("LEVEL0")) {
				logging = Level.SEVERE;
			}
			else if (logLevel.equals("LEVEL1")) {
				logging = Level.INFO;
			}
			else if (logLevel.equals("LEVEL2")) {
				logging = Level.CONFIG;
			}
			else if (logLevel.equals("LEVEL3")) {
				logging = Level.FINE;
			}
			else if (logLevel.equals("LEVEL4")) {
				logging = Level.ALL;
			}
		}
	
		// start a logger
		LOG_FILE = Logger.getLogger("PAWSLog");
		
		try {

			// This block configure the logger with handler and formatter
			
			FileHandler fh = new FileHandler("c:/program files/paws/logs/pawslog.log", true);
			LOG_FILE.addHandler(fh);
			LOG_FILE.setLevel(logging);
			SimpleFormatter formatter = new SimpleFormatter();
			fh.setFormatter(formatter);

	    } 
		catch (SecurityException e) {
			e.printStackTrace();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
